#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
typedef long long LL;
struct node {
	LL x;
	LL cnt;
	node(LL s, LL n) {x=s; cnt=n; }
	node() {}
	LL getSteps() {
		return x*cnt;
	}
	bool operator< (const node& a) const {
		return x<a.x;
	}
	bool operator== (const node& a) const {
		return x==a.x;
	}
};
int main(int argc, char** argv) {
	#ifdef hai
	freopen("in.txt", "r", stdin);
	#endif
	LL n;
	while (cin>>n) {
		LL mark;
		set<node> l1, l2;
		for (LL i=0; i<n; ++i) {
			scanf("%lld", &mark);
			if (mark!=0) l1.insert(node(i, mark));
		}
		for (LL i=0; i<n; ++i) {
			scanf("%lld", &mark);
			if (mark!=0) l2.insert(node(i, mark));
		}
		LL p1=0, p2=0;
		LL len1=l1.size();
		LL len2=l2.size();
		LL res=0;
		for (set<node>::iterator it=l1.begin(); it!=l1.end(); ++it) {
			set<node>::iterator f1=upper_bound(l2.begin(), l2.end(), *it);
			--f1;
			while (it->cnt && f1!=l2.begin()) {
				LL z=min(it->cnt, f1->cnt);
				(it->cnt)-=z;
				(f1->cnt)-=z;
				res+=(it->x - f1->x)*z;
				if (f1->cnt == 0) {
					l2.erase(f1);
					--f1;
				}
			}
		}
		for (set<node>::iterator it=l1.begin(); it!=l1.end(); ++it) res+=(it->getSteps());
		for (set<node>::iterator it=l2.begin(); it!=l2.end(); ++it) res+=(it->getSteps());
		cout<<res<<endl;
	}
	return 0;
}
